package ExpressFour;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.Random;

public class SystemInterface extends JFrame implements ActionListener{
    static class node{
        private final String str;
        private final int bitmap;
        node(String str,int bit){
            this.str=str;
            this.bitmap=bit;
        }

        public int getBitmap() {
            return bitmap;
        }

        public String getStr() {
            return str;
        }
    }
    JTextField jtf_command;
    JButton btn_sure,btn_write;
    JTextArea jta_show,jta_write;
    JTable bit_map;
    FileSystem system;
    JMenuBar jMenuBar;
    JMenu jMenu;
    JPanel panel;
    LinkedList<node>list;
    SystemInterface(){
        list=new LinkedList<>();
        system=new FileSystem();
        panel=new JPanel(new GridLayout(1,3));

        JPanel panel_command=new JPanel(new GridLayout(1,3));
        JPanel panel_show=new JPanel(new GridLayout(1,1));


        jMenuBar=new JMenuBar();
        jMenu=new JMenu();
        jMenu.setText(system.getCurrentPath());
        jMenuBar.add(jMenu);
        setJMenuBar(jMenuBar);


        jta_write=new JTextArea();
        jta_write.setFont(new Font("宋体",Font.BOLD,18));
        jtf_command=new JTextField();
        btn_sure =new JButton("sure");
        btn_sure.addActionListener(this);
        btn_write=new JButton("write");
        btn_write.addActionListener(this);
        getRootPane().setDefaultButton(btn_sure);
        jta_show=new JTextArea();
        jta_show.setEnabled(false);
        jta_show.setDisabledTextColor(Color.black);
        jta_show.setFont(new Font("宋体",Font.BOLD,18));
        jtf_command.setFont(new Font("宋体",Font.BOLD,18));
        btn_write.setFont(new Font("宋体",Font.BOLD,18));
        btn_sure.setFont(new Font("宋体",Font.BOLD,18));

        panel_command.add(jtf_command);
        panel_command.add(btn_sure);
        panel_command.add(btn_write);
        panel_command.setBorder(BorderFactory.createTitledBorder("command input"));

        add(panel_command,BorderLayout.NORTH);
        JPanel panel_write=new JPanel(new GridLayout(1,1));
        panel_write.setBorder(BorderFactory.createTitledBorder("write"));
        panel_write.add(new JScrollPane(jta_write));
        panel.add(panel_write);

        panel_show.add(new JScrollPane(jta_show));
        panel_show.setBorder(BorderFactory.createTitledBorder("show"));
        panel.add(panel_show);


        add(panel,BorderLayout.CENTER);

        init_bitmap();

        setBounds(200,100,800,500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }
    void init_bitmap(){
        bit_map=new JTable(8,8);
        bit_map.setEnabled(false);
        bit_map.setFont(new Font("宋体",Font.BOLD,14));
        JPanel panel_table=new JPanel(new GridLayout(1,1));
        panel_table.add(new JScrollPane(bit_map));
        panel_table.setBorder(BorderFactory.createTitledBorder("bit map"));
        panel.add(panel_table);
        Random random=new Random();
        for(int i=0;i<bit_map.getColumnCount();i++){
            for(int j=0;j<bit_map.getRowCount();j++){
                bit_map.setValueAt(String.valueOf(Math.abs(random.nextInt()%2)),i,j);
            }
        }
    }
    int getFromBitMap(){
        for(int i=0;i<bit_map.getColumnCount();i++){
            for(int j=0;j<bit_map.getRowCount();j++){
                if(bit_map.getValueAt(i,j).equals("0")){
                    bit_map.setValueAt("1",i,j);
                    return i*8+j;
                }
            }
        }
        return -1;
    }
    void setToBitMap(int bit){
        int column=bit%8;
        int row=bit/8;
        bit_map.setValueAt("0",row,column);
    }
    //单指令操作
    void commandOperator(String command){
        switch (command){
            case "ls":
            case "dir":
                jta_show.setText(system.showCurrentPath());
                break;
            case "exit":
                System.exit(0);
            default:
                JOptionPane.showMessageDialog(this,
                        "请正确输入命令",
                        "错误",
                        JOptionPane.ERROR_MESSAGE);
        }
    }
    //双指令操作
    void commandOperator(String command,String command1){
        switch (command){
            case "touch":
                if(!system.addFile(command1)){
                    JOptionPane.showMessageDialog(this,
                            "错误",
                            "error",
                            JOptionPane.ERROR_MESSAGE);
                }
                else{
                    jta_show.setText(command1+"已添加");
                    list.add(new node(command1,getFromBitMap()));
                }
                break;
            case "cd":
                if(command1.equals("..")){
                    jta_show.setText("change path succeed");
                    system.backPath();
                }
                else {
                    boolean flag=false;
                    String[] splits = command1.split("/");
                    //跳转目录
                    for (String split : splits) {
                        if (!system.changePath(split)) {
                            flag=true;
                            jta_show.setText("change path fail.");
                            break;
                        }
                    }
                    if(!flag){
                        jta_show.setText("change path succeed");
                    }
                }
                jMenu.setText(system.getCurrentPath());
                break;
            case "mkdir":
                if(!system.addDir(command1)){
                    JOptionPane.showMessageDialog(this,
                            "错误",
                            "error",
                            JOptionPane.ERROR_MESSAGE);
                }
                else{
                    jta_show.setText(command1+" 已添加");
                    list.add(new node(command1,getFromBitMap()));
                }
                break;
            case "rm":
                if(!system.remove(command1)){
                    JOptionPane.showMessageDialog(this,
                            "错误",
                            "error",
                            JOptionPane.ERROR_MESSAGE);
                }
                else{
                    jta_show.setText(command1+" 已删除");
                    for(int i=0;i<list.size();i++){
                        if(list.get(i).getStr().equals(command1)){
                            setToBitMap(list.get(i).getBitmap());
                            list.remove(i);
                            break;
                        }
                    }
                }
                break;
            case "cat":
                jta_show.setText(system.getSomethingFromFile(command1));
                break;
        }
    }
    public static void main(String[] args) {
        new SystemInterface();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        switch (e.getActionCommand()) {
            case "sure":
                String command=jtf_command.getText();
                jtf_command.setText("");
                String[]split;
                if(command!=null){
                    split=command.split(" ");//根据空格分开
                    switch (split.length){
                        case 1:
                            //单命令操作
                            commandOperator(split[0]);
                            break;
                        case 2:
                            //复命令操作
                            commandOperator(split[0],split[1]);
                            break;
                    }
                }
                break;
            case "write":
                String name=jtf_command.getText();
                jtf_command.setText("");
                if(name!=null&& !name.equals("")){
                    String context=jta_write.getText();
                    jta_write.setText("");
                    if(context!=null&&!context.equals("")){
                        system.addSomethingToFile(name,context);
                        jta_show.setText("write succeed");
                    }
                }
                break;
        }
    }

}
